//
//  MemberRealNameViewController.m
//  DaDaBao
//
//  Created by apple on 15/12/11.
//  Copyright © 2015年 User. All rights reserved.
//

#import "MemberRealNameViewController.h"

#import "CustomTextField.h"

#define maxLength 20

@interface MemberRealNameViewController ()<UIAlertViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UIButton *femaleBtn;
@property(nonatomic,strong)UIButton *maleBtn;
@property(nonatomic,strong)CustomTextField *customText;
@property(nonatomic, strong)CustomTextField *nameText;
@property(nonatomic,strong)CustomTextField * memberText;
@property(nonatomic, assign) int genderInt;
@property(nonatomic,strong)UIView *sexView;
@property(nonatomic,strong)UIView *nameView;
@property(nonatomic,strong)UIView *cardView;
@property(nonatomic,strong)UIView *memberNoView;//工号字段
@property(nonatomic,strong)UIButton *checkBtn;



@end

@implementation MemberRealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    
    TopView *topView =[[TopView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"实名认证"];
    [self.view addSubview:topView];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    
    
    [self createUI];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:TRUE];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloginView) name:@"reLogin" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
}

-(void)reloginView
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登录失败，请重新登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

-(void)createUI
{
    
    _genderInt = 0;
    
    [self.view addSubview:self.nameView];
    [self.view addSubview:self.sexView];
    [self.view addSubview:self.cardView];
    [self.view addSubview:self.memberNoView];
    [self.view addSubview:self.checkBtn];
    
}


-(UIView *)nameView
{
    if (!_nameView) {
        _nameView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 73.0f, SCREEN_WIDTH, 60.0f)];
        _nameView.backgroundColor = [UIColor whiteColor];
        
        UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 60.0f, 30.0f)];
        nameLab.text = @"姓名";
        nameLab.textAlignment = NSTextAlignmentLeft;
        [_nameView addSubview:nameLab];
        
        _nameText = [[CustomTextField alloc] initWithFrame:CGRectMake(80.0f,19.0f, SCREEN_WIDTH - 95.0f, 30.0f)];
        _nameText.placeholder = @"请输入您的真实姓名";
        [_nameText setValue:[UIFont boldSystemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"];
        
        [_nameView addSubview:_nameText];
        
    }
    return _nameView;
}
-(UIView *)sexView
{
    if (!_sexView) {
        _sexView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, self.nameView.frame.origin.y+self.nameView.frame.size.height+8.0f, SCREEN_WIDTH, 60.0f)];
        _sexView.backgroundColor = [UIColor whiteColor];
        
        UILabel *sexLab = [[UILabel alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 60.0f, 30.0f)];
        sexLab.text = @"性别";
        sexLab.textAlignment = NSTextAlignmentLeft;
        [_sexView addSubview:sexLab];
        _maleBtn = [[UIButton alloc] initWithFrame:CGRectMake(80.0f, 20.0f, 20.0f, 20.0f)];
        [_maleBtn addTarget:self action:@selector(buttonMaleTapped:) forControlEvents:UIControlEventTouchUpInside];
        _maleBtn.layer.cornerRadius = 10.0f;
        //    [_maleBtn setBackgroundColor:[UIColor grayColor]];
        [_maleBtn setBackgroundImage:[UIImage imageNamed:@"c"] forState:UIControlStateNormal];
        [_sexView addSubview:_maleBtn];
        
        UILabel *maleLab =[[UILabel alloc ] initWithFrame:CGRectMake(_maleBtn.frame.origin.x+_maleBtn.frame.size.width+5.0f, 20.0f, 20.0f, 20.0f)];
        maleLab.text = @"男";
        [_sexView addSubview:maleLab];
        
        _femaleBtn = [[UIButton alloc] initWithFrame:CGRectMake(sexLab.frame.origin.x+150.0f, 20.0f, 20.0f, 20.0f)];
        [_femaleBtn addTarget:self action:@selector(buttonFemaleTapped:) forControlEvents:UIControlEventTouchUpInside];
        _femaleBtn.layer.cornerRadius = 10.0f;
        [_femaleBtn setBackgroundImage:[UIImage imageNamed:@"c"] forState:UIControlStateNormal];
        [_sexView addSubview:_femaleBtn];
        
        UILabel *femaleLab =[[UILabel alloc ] initWithFrame:CGRectMake(_femaleBtn.frame.origin.x+_femaleBtn.frame.size.width+5.0f, _femaleBtn.frame.origin.y, 20.0f, 20.0f)];
        femaleLab.text = @"女";
        [_sexView addSubview:femaleLab];
        
        
    }
    return _sexView;
}
-(UIView *)cardView
{
    if (!_cardView) {
        _cardView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, self.sexView.frame.origin.y+self.sexView.frame.size.height+8.0f, SCREEN_WIDTH, 60.0f)];
        _cardView.backgroundColor = [UIColor whiteColor];
        
        UILabel *IDCardLab =[[UILabel alloc ] initWithFrame:CGRectMake(15.0f, 15.0f, 100.0f, 30.0f)];
        IDCardLab.text = @"证件号码";
        [_cardView addSubview:IDCardLab];
        _customText = [[CustomTextField alloc] initWithFrame:CGRectMake(110.0f, 19.0f, SCREEN_WIDTH-125.0f, 30.0f)];
        _customText.placeholder = @"请输入您的身份证号码";
        [_customText setValue:[UIFont boldSystemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"];
        
        [_cardView addSubview:_customText];
        
        
    }
    return _cardView;
}
-(UIView *)memberNoView
{
    if (!_memberNoView)
    {
        _memberNoView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, self.cardView.frame.origin.y+self.cardView.frame.size.height+8.0f, SCREEN_WIDTH, 60.0f)];
        _memberNoView.backgroundColor = [UIColor whiteColor];
        
        UILabel *memberNoLabel =[[UILabel alloc ] initWithFrame:CGRectMake(15.0f, 15.0f, 150.0f, 30.0f)];
        memberNoLabel.text = @"员工／经理工号";
        [_memberNoView addSubview:memberNoLabel];
        _memberText = [[CustomTextField alloc] initWithFrame:CGRectMake(160.0f, 19.0f, SCREEN_WIDTH-175.0f, 30.0f)];
        _memberText.delegate = self;
        _memberText.placeholder = @"请输入您的工号";
        [_memberText setValue:[UIFont boldSystemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"];
        _memberText.keyboardType = UIKeyboardTypeNumberPad;
        [_memberNoView addSubview:_memberText];
    }
    return _memberNoView;
}
-(UIButton *)checkBtn
{
    if (!_checkBtn) {
        _checkBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
        _checkBtn.frame = CGRectMake(15.0f, self.memberNoView.frame.origin.y+self.memberNoView.frame.size.height+25.0f, SCREEN_WIDTH-30.0f, 35.0f*HSCALE);
        [_checkBtn setBackgroundColor:[UIColor hexStringToColor:RED_COLOR_VALUE]];
        [_checkBtn setTitle:@"提交审核" forState:UIControlStateNormal];
        _checkBtn.layer.cornerRadius = 5.0f;
        [_checkBtn addTarget:self action:@selector(buttonCheckTapped:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _checkBtn;
}
#pragma mark--UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *a = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (a.length>maxLength) {
        return FALSE;
    }
    
    return TRUE;
}


#pragma mark- 提交审核方法
-(void)buttonCheckTapped:(id)sender
{
    BOOL isIDCard = [_customText checkIDCard:_customText.text];
    BOOL isUserName = [_nameText isUserName:_nameText.text];
    
    if (isUserName == FALSE) {
        ALERT_VIEW(@"请填写正确的姓名");
    }
    else if(_genderInt == 0)
    {
        ALERT_VIEW(@"请选择性别");
    }
    
    else if (isIDCard==FALSE) {
        ALERT_VIEW(@"身份证无效");
    }
    else if (_memberText.text.length <=0) {
    ALERT_VIEW(@"请输入您的工号");
    }

    else{
        [MBProgressHUD showHUDAddedTo:self.view animated:TRUE];
        
        //        发送请求，提交审核  ＊＊ memberNO 这个字段没有写错
        NSDictionary *dict = @{ @"token" :[[NSUserDefaults standardUserDefaults]objectForKey:@"token"], @"Gender" : ( _genderInt == 1 ) ? @"男" : @"女", @"realName" : _nameText.text,
                                @"idCard" : _customText.text,@"memberNO":_memberText.text,@"memberType":@"1"};
        [AppGlobalClass PostDataWithNSstring:REAL_NAME_AUTH_URL withDictionary:dict success:^(NSDictionary *dictionary){
            if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
            {
                [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInt:1] forKey:@"idCardVStatus"];
                [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"memberType"];
                [[NSUserDefaults standardUserDefaults]setObject:_memberText.text forKey:@"memberNo"];
                [[NSUserDefaults standardUserDefaults]setObject:_nameText.text forKey:@"realName"];
                ALERT_VIEW(@"实名认证成功");
                [self.navigationController popViewControllerAnimated:TRUE];
            }
            else
            {
                ALERT_VIEW([dictionary objectForKey:@"message"]);
            }
            [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
            
        } failure:^(NSError *error){
            [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
        } networkerror:^(NSString *networkerror){
            [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
        }];
    }
}

#pragma mark alertDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [AppGlobalClass exitLogin];
    LoginViewController *loginViewCon = [[LoginViewController alloc]init];
    [self presentViewController:loginViewCon animated:TRUE completion:^{}];
    
    NSArray *listArray = self.navigationController.viewControllers;
    
    id tabBar = [listArray objectAtIndex:1];
    if([[tabBar class] isSubclassOfClass:[UITabBarController class]])
    {
        UITabBarController *tabBarCon = tabBar;
        tabBarCon.selectedIndex = 0;
        [self.navigationController popToViewController:[listArray objectAtIndex:1] animated:TRUE];
    }
    else
    {
        UITabBarController *tabBarCon = [listArray objectAtIndex:2];
        tabBarCon.selectedIndex = 0;
        [self.navigationController popToViewController:[listArray objectAtIndex:2] animated:TRUE];
    }
}


#pragma mark-性别按钮
-(void)buttonMaleTapped:(id)sender
{
    
    [_maleBtn setBackgroundImage:[UIImage imageNamed:@"cc"] forState:UIControlStateNormal];
    [_femaleBtn setBackgroundImage:[UIImage imageNamed:@"c"] forState:UIControlStateNormal];
    
    _genderInt = 1;
    
}
-(void)buttonFemaleTapped:(id)sender
{
    [_femaleBtn setBackgroundImage:[UIImage imageNamed:@"cc"] forState:UIControlStateNormal];
    [_maleBtn setBackgroundImage:[UIImage imageNamed:@"c"] forState:UIControlStateNormal];
    
    _genderInt = 2;
}
#pragma mark-返回上一页
-(void)buttonBackTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:TRUE];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
